package com.aiyige.utils;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by xiachao on 2017/10/18.
 */

public class ShareUtil {

    static ShareUtil instance;

    public static ShareUtil getInstance() {
        if (instance == null) {
            instance = new ShareUtil();
        }
        return instance;
    }

    private ShareAction mShareAction;

    private UMAuthListener umAuthListener;

    private CustomShareListener customShareListener;

    public void share(final Activity activity, String url,String title,String detail,String imageUrl) {
        if (activity == null || TextUtils.isEmpty(url)) {
            return;
        }
        if (!url.startsWith("http")) {
            ToastX.show(R.string.invalid_share_url);
            return;
        }
        final UMWeb umWeb = new UMWeb(url);
        umWeb.setTitle(TextUtils.isEmpty(title)?" ":title);
        umWeb.setDescription(TextUtils.isEmpty(detail)?" ":detail);
        if (TextUtils.isEmpty(imageUrl)) {
            umWeb.setThumb(new UMImage(activity, R.mipmap.ic_launcher));
        } else {
            umWeb.setThumb(new UMImage(activity, imageUrl));
        }
        customShareListener = new CustomShareListener(activity);
        mShareAction = new ShareAction(activity)
                .withMedia(umWeb)
                .setDisplayList(
                        SHARE_MEDIA.QQ,
                        SHARE_MEDIA.WEIXIN,
                        SHARE_MEDIA.WEIXIN_CIRCLE)
//        SHARE_MEDIA.SINA
//                .addButton("umeng_sharebutton_copy", "umeng_sharebutton_copy", "umeng_socialize_copy", "umeng_socialize_copy")
//                .addButton("umeng_sharebutton_copyurl", "umeng_sharebutton_copyurl", "umeng_socialize_copyurl", "umeng_socialize_copyurl")
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        if (share_media.name().contains("WEIXIN") && !isWeixinAvilible()) {
                            ToastX.show(MyApp.getAppContext().getString(R.string.weixin_not_install));
                            return;
                        }
                        if (snsPlatform.mShowWord.equals("umeng_sharebutton_copy")) {
                            ToastX.show("umeng_sharebutton_copy");
                        } else if (snsPlatform.mShowWord.equals("umeng_sharebutton_copyurl")) {
                            ToastX.show("umeng_sharebutton_copyurl");
                        } else {
                            new ShareAction(activity).withMedia(umWeb)
                                    .setPlatform(share_media)
                                    .setCallback(customShareListener)
                                    .share();
                        }
                    }
                }).setCallback(customShareListener);
        mShareAction.open();
    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<Activity> mActivity;

        private CustomShareListener(Activity activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
//                ToastX.show(mActivity.get(), platform + " 收藏成功啦");
            } else {
//                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
//                        && platform != SHARE_MEDIA.EMAIL
//                        && platform != SHARE_MEDIA.FLICKR
//                        && platform != SHARE_MEDIA.FOURSQUARE
//                        && platform != SHARE_MEDIA.TUMBLR
//                        && platform != SHARE_MEDIA.POCKET
//                        && platform != SHARE_MEDIA.PINTEREST
//                        && platform != SHARE_MEDIA.INSTAGRAM
//                        && platform != SHARE_MEDIA.GOOGLEPLUS
//                        && platform != SHARE_MEDIA.YNOTE
//                        && platform != SHARE_MEDIA.EVERNOTE) {
//
//                }
//                ToastX.show(mActivity.get(), platform + " 分享成功啦");

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
//                ToastX.show(mActivity.get(), platform + " 分享失败啦");
                if (t != null) {
                    Log.d("throw", "throw:" + t.getMessage());
                }
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastX.show(mActivity.get(), platform + " 分享取消了");
        }
    }

    public static boolean isWeixinAvilible() {
        final PackageManager packageManager = MyApp.getAppContext().getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @return
     */
    public static boolean isQQClientAvailable() {
        final PackageManager packageManager = MyApp.getAppContext().getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
}
